//
//  Fav_shopViewController.swift
//  ShopIOS
//
//  Created by Apple on 2019/8/2.
//  Copyright © 2019 Apple. All rights reserved.
//

import UIKit

class Fav_shopViewController: UIViewController {
    @IBOutlet weak var tableview: UITableView!
    @IBOutlet weak var uv_back: UIView!
    
    var pageNo=1
    var shopList=[Fav_shopData]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        uv_back.addOnClickListener(target: self, action: #selector(back))

        tableview.register(UINib(nibName: "Fav_shopTableViewCell", bundle: nil), forCellReuseIdentifier: "re")
        tableview.separatorStyle = .none
        
        //refresh
        let header = TTRefreshHeader.init(refreshingBlock: {[weak self] in
            guard let strongSelf = self else{return}
            strongSelf.pageNo = 1
            strongSelf.tableview.mj_footer.state = .idle
            strongSelf.tableview.mj_header.endRefreshing()
            strongSelf.loaddata()
        })
        
        tableview.mj_header = header;
        
        let footer = TTRefreshFooter  {  [weak self] in
            guard let strongSelf = self else{return}
            strongSelf.pageNo = strongSelf.pageNo + 1
            strongSelf.loaddata();
        }
        
        tableview.mj_footer = footer
        tableview.mj_footer.isHidden = true
        
        loaddata()
    }
    @objc func back(){
        self.navigationController?.popViewController(animated: true)
    }
    func loaddata(){
        var d = ["app_key":app_key,
                 "pageSize":"20",
                 "pageNo":"\(pageNo)",
            "userkey":userkey!,
            "timestamp":getTimestamp()
            ] as [String : String]
        let sign=SignTopRequest(params: d)
        d["sign"]=sign
        AlamofireHelper.get(url: GetUserCollectionShop, parameters: d, successHandler: {[weak self](res)in
            HUD.dismiss()
            guard let ss = self else {return}
            if ss.pageNo == 1{ ss.shopList.removeAll()}
            
            if ss.tableview.mj_header.isRefreshing(){
                ss.tableview.mj_header.endRefreshing()
            }else if ss.tableview.mj_footer.isRefreshing() {
                ss.tableview.mj_footer.endRefreshing()
                
            }
            let fav_shopResult=Fav_shopResult.init(json: res)
            
            
            if fav_shopResult.Data.count>0 {
                if ss.tableview.mj_footer.isHidden && fav_shopResult.Data.count > 0 {
                    ss.tableview.mj_footer.isHidden = false
                }
                ss.shopList.insert(contentsOf: fav_shopResult.Data, at: ss.shopList.count)
                if ss.shopList.count < 20 {
                    ss.tableview.mj_footer.state = .noMoreData
                }
            }else {
                ss.tableview.mj_footer.state = .noMoreData
            }
            ss.tableview.reloadData()
        }){[weak self] (error) in
            HUD.dismiss()
            guard let ss = self else {return}
            if ss.tableview.mj_header.isRefreshing(){ss.tableview.mj_header.endRefreshing()}
            else if ss.tableview.mj_footer.isRefreshing() {ss.tableview.mj_footer.endRefreshing()}
        }
    }

}
extension Fav_shopViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return shopList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell=tableView.dequeueReusableCell(withIdentifier: "re")as!Fav_shopTableViewCell
        let item=shopList[indexPath.row]
        if item.Logo != nil{
            cell.iv_logo.setImage(path: URL.init(string: item.Logo!)!)
        }
        cell.lb_shopname.text=item.Name
        cell.selectionStyle = .none
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
}
